<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\api\model\user\UserInvoice as UserInvoiceModel;

/**
 * 发票抬头接口
 */
class Userinvoice extends Api
{
    //protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 列表
     *
     */
    public function lists()
    {
        $list = UserInvoiceModel::where(['user_id'=>$this->auth->id])->select();

        $this->success('请求成功',$list);
    }

    //添加
    public function add()
    {
        $account_model = new UserInvoiceModel();

        $data = [
            'user_id'=>$this->auth->id,
            'name'=>input('name',''),
            'credit_code'=>input('credit_code',''),
            'address'=>input('address',''),
            'mobile'=>input('mobile',''),
            'bank'=>input('bank',''),
            'bank_account'=>input('bank_account',''),
        ];
        if (empty($data['name']))$this->error('请输入名称');
        if (empty($data['credit_code']))$this->error('请输入信用代码');
        if (empty($data['address']))$this->error('请输入单位地址');
        if (empty($data['mobile']))$this->error('请输入电话号码');
        if (empty($data['bank']))$this->error('请输入开户银行');
        if (empty($data['bank_account']))$this->error('请输入银行账户');

        $res = $account_model->save($data);

        $this->success('请求成功',$res);
    }

    //银行账号信息
    public function info()
    {
        $id = input('id',0);

        $info = UserInvoiceModel::get($id);

        if (!$info)$this->error('不存在');

        $this->success('请求成功',$info);
    }

    //删除
    public function del()
    {
        $id = input('id',0);

        $info = UserInvoiceModel::get($id);

        if (!$info)$this->error('不存在');

        $res = $info->delete();

        if ($res){
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    //编辑
    public function edit()
    {
        $id = input('id',0);

        $account_model = new UserInvoiceModel();

        $data = [
            'name'=>input('name',''),
            'credit_code'=>input('credit_code',''),
            'address'=>input('address',''),
            'mobile'=>input('mobile',''),
            'bank'=>input('bank',''),
            'bank_account'=>input('bank_account',''),
        ];
        if (empty($data['name']))$this->error('请输入名称');
        if (empty($data['credit_code']))$this->error('请输入信用代码');
        if (empty($data['address']))$this->error('请输入单位地址');
        if (empty($data['mobile']))$this->error('请输入电话号码');
        if (empty($data['bank']))$this->error('请输入开户银行');
        if (empty($data['bank_account']))$this->error('请输入银行账户');

        $res = $account_model->save($data,['id'=>$id]);

        $this->success('编辑成功');
    }


}
